/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2023-2023. All rights reserved.
 */

import { TrackAPI } from '../../../common/trace/hvigor-trace-factory.js';
import { CoreTask } from '../../../external/task/core-task.js';
import { Task } from '../interface/task.js';

/**
 * 对外提供的Task视图接口实现
 */
export class TaskImpl implements Task {
  private readonly task: CoreTask;

  constructor(task: CoreTask) {
    this.task = task;
  }

  @TrackAPI getDependencies(): string[] {
    return this.task.getDependsOn().map((value: string) => {
      // 去除任务名前面的（:）冒号
      if (value && value.startsWith(':')) {
        return value.substring(1, value.length);
      }
      return value;
    });
  }

  @TrackAPI getName(): string {
    return this.task.getName();
  }

  @TrackAPI setEnable(enable: boolean): void {
    this.task.setEnabled(enable);
  }

  @TrackAPI beforeRun(fn: Function): void {
    this.task.beforeRun(fn);
  }

  @TrackAPI afterRun(fn: Function): void {
    this.task.afterRun(fn);
  }
}
